/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imat;

import java.awt.Dimension;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import se.chalmers.ait.dat215.project.Product;

/**
 *
 * @author sunding
 */
public class FavoritesPresenter {
    private FavoritesView favoritesView;

    public FavoritesPresenter(FavoritesView view) {
        favoritesView = view;
    }

    public void updateFavorites() {
        favoritesView.getProductsPanel().removeAll();
        List<Product> favorites = DataHandler.getInstance().favorites();
        for (Product p : favorites) {
            addFavorite(p);
        }
        if (DataHandler.getInstance().favorites().isEmpty()) {
            favoritesView.getProductsPanel().add(new JLabel("Du har inte sparat några favoriter ännu"));
        }

        int cap = favorites.size();
        JPanel panel = favoritesView.getScrollPanel();
        System.out.println("panelSize: "+panel.getSize());
        int w = panel.getSize().width;
        int nrW = 1;
        if (w >= 795)
            nrW = 3;
        else if (w >= 530)
            nrW = 2;
        int rows = (cap / nrW) + 1;
        panel.setPreferredSize(new Dimension(0, 10+rows*130));
        panel.revalidate();
        System.out.println("panelPreferredSize: "+panel.getPreferredSize());

        favoritesView.repaint();
    }

    public void addFavorite(Product p) {
        ProductView view = new ProductView();
        view.setProduct(p);
        favoritesView.getProductsPanel().add(view);
        favoritesView.validate();
        
    }

}
